//Created by Studentloon, using Garry's manhack spawner as a base
// Variables that are used on both client and server

SWEP.Author			= "Fadi"
	SWEP.Category			= "Fadi's Admin Weapons"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true		// Spawnable in singleplayer or by server admins

SWEP.ViewModel			= "models/weapons/v_IRifle.mdl"
SWEP.WorldModel			= "models/weapons/w_IRifle.mdl"

SWEP.HoldType = "ar2"

SWEP.ViewModelFOV = 57
SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"
local ShootSound = Sound( "Weapon_AR2.Single" )
local ReloadSound = Sound("Weapon_AR2.Reload")


//Editable variables


local cagedoordist = 30	//distance from the victim
local cagedoorheight = 70 //height above the victim (usually ground zero for npcs and players)
local cageceilingheight = 50 //offset for the ceiling
local cagetime = 30 //time for a non-perma-cage to stick around for
local doormodel = "models/props_wasteland/prison_celldoor001a.mdl" //sides of the cage
local ceilingmodel = "models/props_wasteland/prison_cellwindow002a.mdl" //roof of the cage



/*---------------------------------------------------------
	Reload does nothing
---------------------------------------------------------*/
function SWEP:Reload()
	self.Weapon:EmitSound( ReloadSound )
	local find = ents.FindByName("cagedoorsection")
	for k,v in ipairs(find) do
	v:Remove()
	end
end

/*---------------------------------------------------------
   Think does nothing
---------------------------------------------------------*/
function SWEP:Think()	
end


/*---------------------------------------------------------
	PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()

	local tr = self.Owner:GetEyeTrace()
	if ( !tr.Hit ) then return end
	
	local effectdata = EffectData()
		effectdata:SetOrigin( tr.HitPos )
		effectdata:SetNormal( tr.HitNormal )
		effectdata:SetMagnitude( 8 )
		effectdata:SetScale( 1 )
		effectdata:SetRadius( 16 )
	util.Effect( "AR2Impact", effectdata )
	
	self.Weapon:EmitSound( ShootSound )
	self.BaseClass.ShootEffects( self )
	
	if (tr.Entity:IsValid()) then
	local tempdoor = makecage(tr.Entity:GetPos(), tr.Entity)
	for j = 1,4 do
		tempdoor[j]:Fire("kill","",cagetime)
	end
		tempdoor[5]:Fire("kill","",(cagetime + 2))
		tempdoor[5]:SetOwner(self.Owner)
	end
end

/*---------------------------------------------------------
	SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()

	local tr = self.Owner:GetEyeTrace()
	if ( !tr.Hit ) then return end
	
	local effectdata = EffectData()
		effectdata:SetOrigin( tr.HitPos )
		effectdata:SetNormal( tr.HitNormal )
		effectdata:SetMagnitude( 8 )
		effectdata:SetScale( 1 )
		effectdata:SetRadius( 16 )
	util.Effect( "AR2Impact", effectdata )
	
	self.Weapon:EmitSound( ShootSound )
	self.BaseClass.ShootEffects( self )
	
	if (tr.Entity:IsValid()) then
	tr.Entity:GetPos()
	local permdoor = makecage(tr.Entity:GetPos(), tr.Entity)
	end
end

function makecage(cageposinit, entity)
local cagepos = cageposinit
cagepos.z = cagepos.z + cagedoorheight
local vicang = Vector(3,3,3)
local vicpos = Vector(3,3,3)
local name = "cagedoorsection"
doors = {}
for i = 1,4 do
vicpos = cagepos
			if (i == 1) then							--set up the angle for the next door and name it
				vicang = Vector(0.0,0.0,0.0)
				vicpos.x = vicpos.x + cagedoordist
			end
			if (i == 2) then
				vicang = Vector(0.0,180.0,0.0)
				vicpos.x = vicpos.x - cagedoordist*2
			end
			if (i == 3) then
				vicang = Vector(0.0,90.0,0.0)
				vicpos.y = vicpos.y + cagedoordist
				vicpos.x = vicpos.x + cagedoordist
			end
			if (i == 4) then
				vicang = Vector(0.0,270.0,0.0)
				vicpos.y = vicpos.y - cagedoordist*2
			end
	local door = ents.Create("prop_physics") 
	door:SetModel(doormodel)
    door:SetPos(vicpos)
    door:SetAngles(vicang)
	door:SetName(name)
    door:Spawn()
	door:GetPhysicsObject():SetMass(2000)
	door:Activate()
	table.insert(doors,i,door)
end
	cagepos.z = cagepos.z + cageceilingheight
	cagepos.y = cagepos.y + cagedoordist
	local ceiling = ents.Create("prop_physics") 
	ceiling:SetModel(ceilingmodel)
    ceiling:SetPos(cagepos)
    ceiling:SetAngles(Vector(90.0,90.0,0.0))
	ceiling:SetName(name)
    ceiling:Spawn()
	ceiling:GetPhysicsObject():SetMass(2000)
	ceiling:Activate()
	table.insert(doors,5,ceiling)
	
	constraint.Weld( doors[1], doors[2], 0,0, 0 )
	constraint.Weld( doors[2], doors[3], 0,0, 0 )
	constraint.Weld( doors[3], doors[4], 0,0, 0 )
	constraint.Weld( doors[4], doors[5], 0,0, 0 )
	constraint.Weld( doors[5], doors[1], 0,0, 0 )
 return doors
end

